<?php
namespace Admin\Controller;
use Admin\Controller\AdminController;
use Common\Model\CaozuologModel;
class ProblemController extends AdminController{
	public function _initialize(){
		parent::_initialize();
	}
	public function getAnswerList(){
		$list=M('answer')->select();
		$return = [
		'data'=>$list,
		'status'=>1
		];
		$this->ajaxReturn($return);
	}
	public function getAnswerDetail(){
		$id=I('id');
		$list=M('answer')->where('id ='.$id)->find();
		$return = [
		'data'=>$list,
		'status'=>1
		];
		$this->ajaxReturn($return);
	}
	public function addAnswer(){
		$id=I('id');
		$keyword=I('keyword');
		$content=$_POST['content'];
		if(empty($id)){
			$arr['keyword']=$keyword;
			$arr['answer']=$content;
			$r=M('answer')->add($arr);
		}else{
			$arr['keyword']=$keyword;
			$arr['answer']=$content;
			$r=M('answer')->where('id ='.$id)->save($arr);
		}
		if($r){
			$M_LOG = new CaozuologModel();
			$content = '添加/修改关键词回复';
			$M_LOG->addLog($this->user['username'], $content);
			$msg['status']=1;
			$msg['msg']='操作成功';
			$this->ajaxReturn($msg);
		}else{
			$msg['status']=0;
			$msg['msg']='服务器繁忙，请稍后再试';
			$this->ajaxReturn($msg);
		}
	}
	public function delAnswer(){
		$id=I('id');
		$r=M('answer')->where('id ='.$id)->delete();
		if($r){
			$M_LOG = new CaozuologModel();
			$content = '删除关键词回复';
			$M_LOG->addLog($this->user['username'], $content);
			$msg['status']=1;
			$msg['msg']='操作成功';
			$this->ajaxReturn($msg);
		}else{
			$msg['status']=0;
			$msg['msg']='服务器繁忙，请稍后再试';
			$this->ajaxReturn($msg);
		}
	}
	/**
	 * 反馈列表
	 */
	public function getProblemList(){
		$order = '';
		$sort = I('sort');
		if(!empty($sort)){
			if(!in_array($sort, [1,2])){
				$data = [
				'status'=>-1,
				'msg'=>'参数错误，排序参数错误'
						];
				$this->ajaxReturn($data);
			}
			$sort==1?$order = 'o.add_time':$order = 'o.add_time desc';
			 
		}
		$uid=I('uid');
		$username=I('username');
		$status=I('status');
		$type=I('type');
		$phone=I('phone');
		$begin = I('begin');
		$last = I('last');
		$page = I('page');
		$num = I('num');
		$admin = $this->user;
		$where = [];
		if(!empty($uid)){
			$where['o.uid'] = $uid;
		}
		if(!empty($username)){
			$where['u.loginName'] = array('like','%'.$username.'%');
		}
		if(!empty($phone)){
			$where['u.userPhone'] = $phone;
		}
		if(!empty($status)){
			$where['o.status'] = $status;
		}
		if(!empty($type)){
			$where['o.type'] = $type;
		}
		$whereTime = $this->dotime($begin, $last);
		$where['o.add_time'] = array('between',[$whereTime['begin'],$whereTime['last']]);
		$pages = $this->DoPage($page, $num);
		$list = M('problem')
				->alias('o')
				->field('o.*,u.loginName,u.userPhone')
				->join('__USERS__ as u on u.userid = o.uid')
				->where($where)
				->limit($pages['limit'],$pages['num'])
				->order($order)
				->select();
		$count =M('problem')
				->alias('o')
				->join('__USERS__ as u on u.userid = o.uid')
				->where($where)
				->count();
		$return = [
		'count'=>$count,
		'data'=>$list,
		'status'=>1
		];
		$this->ajaxReturn($return);
		
	}
	/**
	 * 回复反馈
	 */
	public function answer(){
		$id=I('id');
		$content=I('content');
		$problem=M('problem')->where('id ='.$id)->find();
		if(empty($id)){
			$data = [
			'status'=>-1,
			'msg'=>'参数错误'
					];
			$this->ajaxReturn($data);
		}
		if(empty($content)){
			$data = [
			'status'=>-1,
			'msg'=>'回复内容不能为空'
					];
			$this->ajaxReturn($data);
		}
		if(!$problem){
			$data = [
			'status'=>-1,
			'msg'=>'参数错误'
					];
			$this->ajaxReturn($data);
		}
		$arr['answer']=$content;
		$arr['check_time']=time();
		$arr['status']=1;
		$r=M('problem')->where('id ='.$id)->save($arr);
		if($r){
			$M_LOG = new CaozuologModel();
			$content = '回复前台反馈，ID为'.$id;
			$M_LOG->addLog($this->user['username'], $content);
			$msg['status']=1;
			$msg['msg']='修改成功';
			$this->ajaxReturn($msg);
		}else{
			$msg['status']=0;
			$msg['msg']='服务器繁忙，请稍后再试';
			$this->ajaxReturn($msg);
		}
		
	}
}